package cn.dujc.superkid.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import cn.dujc.superkid.R;
import cn.dujc.superkid.bean.UnitTestResult;
import cn.dujc.superkid.utils.DateUtil;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by Administrator on 2018/10/15 0015.
 */

public class UnitTestMsgAdapter extends RecyclerView.Adapter<UnitTestMsgAdapter.ViewHolder> {
    private Context context;
    private List<UnitTestResult.DataBean.MessageListBean> datas;
    private String avatar = "http://api.niuwaketang.com/avatar/avatar.png";
    private String teacherAvatar = "http://api.niuwaketang.com/avatar/teacher_avatar.png";

    public void setDatas(List<UnitTestResult.DataBean.MessageListBean> datas) {
        this.datas = datas;
        notifyDataSetChanged();
    }

    public UnitTestMsgAdapter(Context context) {
        this.context = context;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_message_entity, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        UnitTestResult.DataBean.MessageListBean item = datas.get(position);
        holder.userName.setText(item.getUsername());
        holder.userTime.setText(DateUtil.formatData("yyyy-MM-dd HH:mm:ss", item.getPosttime()));
        holder.userMessage.setText(Html.fromHtml(item.getContent()));
        Glide.with(context).load(avatar).into(holder.userAvatar);
        if (item.getStatus() == 1) {
            holder.teacherGroup.setVisibility(View.GONE);
            holder.waitGroup.setVisibility(View.VISIBLE);
        } else {
            holder.teacherGroup.setVisibility(View.VISIBLE);
            holder.waitGroup.setVisibility(View.GONE);
            holder.teacherName.setText(item.getTeacher_name());
            Glide.with(context).load(teacherAvatar).into(holder.teacherAvatar);
            holder.teacherMessage.setText(Html.fromHtml(item.getAnswer_content()));
            holder.teacherTime.setText(DateUtil.formatData("yyyy-MM-dd HH:mm:ss", item.getAnswer_posttime()));

        }
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        CircleImageView userAvatar, teacherAvatar;
        TextView userName, userTime, userMessage;
        TextView teacherName, teacherTime, teacherMessage;
        View teacherGroup, waitGroup;

        public ViewHolder(View itemView) {
            super(itemView);
            userAvatar = itemView.findViewById(R.id.item_message_avatar);
            teacherAvatar = itemView.findViewById(R.id.item_message_teacher_avatar);
            userName = itemView.findViewById(R.id.item_message_username);
            userTime = itemView.findViewById(R.id.item_message_user_time);
            userMessage = itemView.findViewById(R.id.item_message_user_message);
            teacherName = itemView.findViewById(R.id.item_message_teacher_name);
            teacherTime = itemView.findViewById(R.id.item_message_teacher_time);
            teacherMessage = itemView.findViewById(R.id.item_message_teacher_message);
            teacherGroup = itemView.findViewById(R.id.item_message_teacher_group);
            waitGroup = itemView.findViewById(R.id.item_message_is_wait);
        }
    }
}
